<?php

namespace app\admin\controller;

use app\common\controller\Backend;

class Product extends Backend{
    public function __construct()
    {
        parent::__construct();

        $this->ProductModel=model('Product.Product');
        $this->TypeModel=model('Product.Type');

    }

    public function index(){
        $productlist=$this->ProductModel->with(['type'])->paginate(10);

        $count=$this->ProductModel->count();

        $pagestr=$productlist->render();

        $this->assign([
            'productlist'=>$productlist,
            'count'=>$count,
            'pagestr'=>$pagestr,
        ]);

        return $this->fetch();
    }

    public function add(){
        if($this->request->isPost()){
            $params=$this->request->param();

            $data=[
                'name'=>$params['name'],
                'typeid'=>$params['typeid'],
                'price'=>$params['price'],
                'stock'=>$params['stock'],
                'content'=>$params['content'],
                'flag'=>$params['flag'],
            ];

            if($_FILES['thumbs']['size'] > 0){
                $result=uploads('thumbs');

                if($result===FALSE){
                    $this->error('上传文件失败');
                    exit;
                }

                $data['value']=$result;
            }

            $Product=$this->ProductModel->validate('common/Product/Product')->save($data);

            if($Product===FALSE){
                $this->error($this->ProductModel->getError());
                exit;
            }else{
                $this->success('添加商品成功',url('home/Product/index'));
                exit;
            }
        }

        // 查询所有商品分类
        $typelist=$this->TypeModel->select();

        // 获取附加功能
        // $flaglist=$this->ProductModel->flag();

        $this->assign([
            'typelist'=>$typelist,
            // 'flaglist'=>$flaglist,
        ]);

        return $this->fetch();
    }

    public function edit(){
        

        return $this->fetch();
    }
}